# -*- coding: utf-8 -*-
# @Time    : 2020/5/21 10:12
# @Author  :
# @File    : down_routers.py
# @Software: PyCharm
import datetime
import json
import re
import traceback
from parsel import Selector
import requests
from fastapi import APIRouter, Form

from app.allsubdb.paper_jingjiribao.models import PaperJinjiribao, PaperJinjiribaoModel, UpdateHtml
from app.core.return_info import ReturnInfo, SUCCESS, FAILED, MsgCode

router = APIRouter()


def checkExist(obj):
    if obj != None and len(obj) > 0:
        return True
    else:
        return False


@router.post("/get_day_link")
def get_day_link(*, proxy: str = Form(None), years: str = Form(None), months: str = Form(None)):
    """
    经济日报根据年和月返回当月每日的报纸链接
    :param proxy: 代理
    :param years: 年份:2008
    :param months: 月份:01
    :return:
    """
    if len(months) == 1:
        months = "0" + months
    rtn = ReturnInfo()
    headers = {
        "Accept": "text/javascript, text/html, application/xml, text/xml, */*",
        "Host": "paper.ce.cn",
        "X-Requested-With": "XMLHttpRequest",
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'

    }
    proxies = None
    if proxy:
        proxies = {
            'http': proxy,
            'https': proxy
        }
    r = None
    try:
        r = requests.get(
            url="http://paper.ce.cn/jjrb/html/{}-{}/period.xml".format(years, months),
            headers=headers,
            proxies=proxies
        )
    except:
        print(traceback.format_exc())
        rtn.status = FAILED
        rtn.msg = "请求错误，没有返回Response"
        return rtn
    if r.status_code != 200:
        rtn.msg_code = r.status_code
        rtn.status = FAILED
        rtn.msg = "返回状态码错误"
        return rtn
    content = r.content
    if content.startswith(b'\xef\xbb\xbf'):  # 去掉 utf8 bom 头
        content = content[3:]
    htmlText = content.decode('utf8').strip()
    if htmlText.find("periodlist") < 0:
        rtn.msg_code = r.status_code
        rtn.status = FAILED
        rtn.msg = "查询网页特征值错误"
        return rtn
    sel = Selector(text=htmlText)
    list_period = sel.xpath("//periodlist/period")
    if len(list_period) == 0:
        rtn.status = FAILED
        rtn.msg = "当前月份无报纸链接"
        return rtn
    list_link = []
    for per in list_period:
        periodDate = per.xpath("./period_date/text()").extract()[0].strip()
        tmp = periodDate.split("-")
        list_link.append({
            "periodDate": periodDate.replace("-", ""),
            "url": "http://paper.ce.cn/jjrb/html/{}-{}/{}/".format(tmp[0], tmp[1], tmp[2]) +
                   per.xpath("./front_page/text()").extract()[0].strip()
        })
    rtn.status = SUCCESS
    rtn.msg = "下载成功"
    dic = {
        "years": years,
        "months": months,
        "count": len(list_link),
        "dayLink": list_link
    }
    rtn.data = dic
    return rtn.todict()

